package com.sinya.shoppingmall;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.sinya.shoppingmall.bean.Constants;
import com.sinya.shoppingmall.fragment.BrandFragment;
import com.sinya.shoppingmall.fragment.DiscountFragment;
import com.sinya.shoppingmall.fragment.NewSaleFragment;
import com.sinya.shoppingmall.fragment.PanicBuyFragment;
import com.sinya.shoppingmall.fragment.SeasonalFragment;
import com.sinya.shoppingmall.implement.GetDataInterface;

/**
 * 通用的商品显示Activity
 * ①启动前需要传递相关的模块信息，以帮助打开对应的Frgament
 *
 * @author KoizumiSinya
 * @date 2016/1/13.
 */
public class CommodityActivity extends BaseFragmentActivity {

    /**
     * 传递数据的 bundle
     */
    private Bundle dataBundle;

    /**
     * 标题文本
     */
    private String title;

    /**
     * 启动模块判定依据的 id
     */
    private int fragmentId;

    // [+] Override Method

    @Override
    protected void initData() {
        getBundleData();
    }

    @Override
    protected Fragment setCustomFragment() {
        return createFragment(fragmentId);
    }

    @Override
    protected String setTitle() {
        return title;
    }

    //TODO 为什么要在要调用接口GetDataInterface？
    @Override
    protected void initViews(View rootView) {

        //根据实现接口的方法，来获取请求数据响应
        if(currentFragment instanceof GetDataInterface){
            ((GetDataInterface) currentFragment).fristGetData();
        }
    }

    // [-] Override Method

    /**
     * 通过Bundle获取进入 CommodityActivity 时传递来的数据
     */
    private void getBundleData() {
        dataBundle = getIntent().getExtras();
        if (dataBundle != null) {
            title = dataBundle.getString(Constants.BUNDLE_FIELD_TITLE);
            fragmentId = dataBundle.getInt(Constants.BUNDLE_FIELD_INDEX);
        }
    }

    /**
     * 根据模块id 创建Fragment
     *
     * @param id
     * @return
     */
    private Fragment createFragment(int id) {
        if (id == Constants.DISCOUNT) {
            //打折促销
            return new DiscountFragment();
        } else if (id == Constants.NEW_SALE) {
            //新品发布
            return new NewSaleFragment();
        } else if (id == Constants.SEASONAL) {
            //换季降价
            return new SeasonalFragment();
        } else if (id == Constants.PANIC_BUYING) {
            //限时抢购
            return new PanicBuyFragment();
        } else if (id == Constants.BRAND) {
            //正品专柜
            return new BrandFragment();
        } else {
            return null;
        }
    }
    // [+] click

    // [-] click
}
